﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Coding4FunCamera
{
    public partial class BrowsePictures : Form
    {
        private string _initialDirectory;
        public BrowsePictures(string directory)
        {
           
            InitializeComponent();
            InitializeViewer(directory);
        }

        private void InitializeViewer(string directory)
        {
            bool filesFound = false;
            dropdownPictures.Items.Clear();
            InitialDirectory = directory;
            string[] files = Directory.GetFiles(InitialDirectory);
            foreach (string file in files)
            {
                FileInfo fileinfo = new FileInfo(file);
                if (fileinfo.Extension.ToLower() == ".jpg" ||
                    fileinfo.Extension.ToLower() == ".bmp" ||
                    fileinfo.Extension.ToLower() == ".jpeg")
                {
                    dropdownPictures.Items.Add(fileinfo.Name);
                    filesFound = true;
                }

            }

            if (filesFound)
            {
                string file = files[0];
                Bitmap picture = new Bitmap(file);
                pictureViewer.Image = picture;
                dropdownPictures.SelectedIndex = 0;
            }
        }
        public string InitialDirectory
        {
            get
            {
                return _initialDirectory;
            }
            set
            {
                string testDirectory = value;
                if (Directory.Exists(testDirectory))
                {
                    _initialDirectory = value;
                }
                else
                {
                    throw new ApplicationException(Coding4FunCamera.Properties.Resources.InvalidDirectory);
                }
            }
        }

        private void menuBack_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void dropdownPictures_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fileName = InitialDirectory + "\\" + (string)dropdownPictures.SelectedItem;
            if (File.Exists( fileName))
            {
                Bitmap picture = new Bitmap(fileName);
                pictureViewer.Image = picture;
                pictureViewer.Refresh();
            }
        }

        private void menuDelete_Click(object sender, EventArgs e)
        {
            string fileName = InitialDirectory + "\\" + (string)dropdownPictures.SelectedItem;
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            InitializeViewer(_initialDirectory);
        }

      

    }
}